import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class callableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("执行任务...");
                int sum = 0;
                //完成累加操作
                for (int i = 1; i <= 10; i++) {
                    sum +=i;
                }
                TimeUnit.SECONDS.sleep(1);
                System.out.println("运算结束..."+"sum = " + sum);
                return sum;
            }
        };
        //搭配Future使用，获取Callable的执行结果
        FutureTask<Integer> futureTask  = new FutureTask<>(callable);

        Thread t1 = new Thread(futureTask);
        t1.start();

        //等待结果的时间可能会被中断，会抛出InterruptedException异常
        Integer integer = futureTask.get();
        System.out.println(integer);
    }
}
